﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WLYT.Application.Interfaces;
using WLYT.Application.Services;
using WLYT.Domain.Entries;
using WLYT.Domain.Model;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace WLYT.WMS.Controllers
{
    [ApiController]
    [Authorize]
    public class InventoryController(IBaseService<Inventory> inventoryService, IBaseService<InventoryLog> inventoryLogService) : ControllerBase
    {
        /// <summary>
        /// 库存查询
        /// </summary>
        /// <param name="reelId">ReelID</param>
        /// <param name="originalCode">料号</param>
        /// <param name="locationNo">库位</param>
        /// <param name="warehouseNo">仓库</param>
        /// <param name="status">状态</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="pageSize">数量</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Inventory/GetData")]
        public async Task<Table<Inventory>> GetData(string reelId = "", string originalCode = "", string locationNo = "", string warehouseNo = "", DateTime? startDate = null, DateTime? endDate = null, int? status = null, int pageSize = 20, int pageIndex = 1)
        {
            Expressionable<Inventory> exp = Expressionable.Create<Inventory>();
            exp.AndIF(!string.IsNullOrEmpty(reelId), p => p.ReelId.Contains(reelId));
            exp.AndIF(!string.IsNullOrEmpty(originalCode), p => p.OriginalCode.Contains(originalCode));
            exp.AndIF(!string.IsNullOrEmpty(locationNo), p => p.LocationNo.Contains(locationNo));
            exp.AndIF(!string.IsNullOrEmpty(warehouseNo), p => p.WarehouseNo == warehouseNo);
            exp.AndIF(status != null, p => p.Status == status);
            exp.AndIF(startDate != null, p => p.CreateTime >= startDate);
            exp.AndIF(endDate != null, p => p.CreateTime <= endDate);
            var result = await inventoryService.GetPageListAsync(exp, pageSize, pageIndex);
            return new Table<Inventory>(result.Item1, result.Item2);
        }

        /// <summary>
        /// 库存汇总
        /// </summary>
        /// <param name="originalCode"></param>
        /// <param name="warehouseNo"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Inventory/GetMaterials")]
        public async Task<Table<object>> GetMaterials(string originalCode = "", string warehouseNo = "", int pageSize = 20, int pageIndex = 1)
        {
            string whereSql = "";
            if (!string.IsNullOrEmpty(warehouseNo))
            {
                whereSql += $" AND i.warehouseNo = '{warehouseNo}'";
            }
            string sql = $@"SELECT i.OriginalCode, SUM(i.Qty) Qty,COUNT(1) Count FROM Inventory i WHERE i.Qty > 0 {whereSql} GROUP BY i.OriginalCode";
            RefAsync<int> total = 0;
            var list = await inventoryService.AsSugarClient().SqlQueryable<object>(sql)
                .WhereIF(!string.IsNullOrEmpty(originalCode), "originalCode=@originalCode", new { originalCode })
                .OrderBy("Count desc")
                .ToPageListAsync(pageIndex, pageSize, total);
            return new Table<object>(list, total);
        }

        /// <summary>
        /// 库存记录
        /// </summary>
        /// <param name="reelId"></param>
        /// <param name="originalCode"></param>
        /// <param name="locationNo"></param>
        /// <param name="warehouseNo"></param>
        /// <param name="type"></param>
        /// <param name="orderNo"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/InventoryLog/GetData")]
        public async Task<Table<InventoryLog>> GetData(string reelId = "", string originalCode = "", string locationNo = "", string warehouseNo = "", int? type = null, string orderNo = "", DateTime? startDate = null, DateTime? endDate = null, int pageSize = 20, int pageIndex = 1)
        {
            Expressionable<InventoryLog> exp = Expressionable.Create<InventoryLog>();
            exp.AndIF(type != null, p => p.OrderType == type);
            exp.AndIF(!string.IsNullOrEmpty(reelId), p => p.ReelId.Contains(reelId));
            exp.AndIF(!string.IsNullOrEmpty(originalCode), p => p.OriginalCode.Contains(originalCode));
            exp.AndIF(!string.IsNullOrEmpty(locationNo), p => p.LocationNo == locationNo);
            exp.AndIF(!string.IsNullOrEmpty(warehouseNo), p => p.WarehouseNo == warehouseNo);
            exp.AndIF(!string.IsNullOrEmpty(orderNo), p => p.OrderNo == orderNo);
            exp.AndIF(startDate != null, p => p.CreateTime >= startDate);
            exp.AndIF(endDate != null, p => p.CreateTime <= endDate);
            var result = await inventoryLogService.GetPageListAsync(exp, pageSize, pageIndex);
            return new Table<InventoryLog>(result.Item1, result.Item2);
        }
    }
}
